define(['../module','../../services/chartPage/page-service'],
    function (controllers) {
        "use strict";
        controllers.controller('PageCtrl', ['$scope', 'pageService', 'chartService', function ($scope, pageService, chartService) {

            if (!$scope.chartTemplate) {
                //get the chart template for this view... right now it covers all charts...
                pageService.get(null, '../highcharts.json').then(function (success) {
                    console.log("success:");
                    console.log(success);
                    $scope.chartTemplate = success;
                    $scope.createCharts();
                }, function (fail) {
                    console.log('Problem getting chart template', fail)
                });
            } else {
                $scope.createCharts();
            }

            $scope.createCharts = function () {

                var dataDescription = {
                    timeseries: {
                        yAxisLabels: [''],
                        labels: ['Total'],
                        dataAttr: ['label', ['timestamp', 'count']],
                        detailDataAttr: ['count'],
                        colors: ['rgba(43,166,203,0.9)',
                            'rgba(52,52,52,0.3)',
                            'rgba(169,234,181,0.9)',
                            'rgba(43,43,43,0.5)'],
                        borderColor: '#1b97d1',
                        areaColors: ['rgba(43,166,203,0.9)',
                            'rgba(52,52,52,0.3)',
                            'rgba(169,234,181,0.9)',
                            'rgba(43,43,43,0.5)']
                    },
                    pie: {
                        dataAttr: ['label', ['timestamp', 'count']],
                        colors: [
                            'rgba(43,166,203,0.4)',
                            'rgba(52,52,52,0.3)',
                            'rgba(169,234,181,0.5)',
                            'rgba(43,43,43,0.5)',
                            '#ff9191', '#ffa1a1', '#ffb6b6', '#ffcbcb'],
                        borderColor: '#ff0303'
                    },
                    bar: {
                        labels: ['barexample'],
                        dataAttr: ['label', 'value'],
                        colors: [createGradient('rgba(43,166,203,0.9)', 'rgba(43,166,203,0.4)')],
                        borderColor: 'rgba(43,166,203,1)'
                    }
                };

                var pieChartData = [
                    {
                        label: '华安大中华升级',
                        datapoints: [
                            {
                                timestamp: 1383230753238,
                                count: 198.81
                            }
                        ]
                    },
                    {
                        label: '长盛电子信息产业',
                        datapoints: [
                            {
                                timestamp: 1383230753238,
                                count: 376.71
                            }
                        ]
                    },
                    {
                        label: '长盛量化红利',
                        datapoints: [
                            {
                                timestamp: 1383230753238,
                                count: 209.74
                            }
                        ]
                    },
                    {
                        label: '国泰金鹰增长',
                        datapoints: [
                            {
                                timestamp: 1383230753238,
                                count: 209.47
                            }
                        ]
                    },
                    {
                        label: '华商主题精选股票',
                        datapoints: [
                            {
                                timestamp: 1383230753238,
                                count: 313.86
                            }
                        ]
                    }

                ]

                var lineChartData = [
                    {
                        label: '长盛电子信息产业',
                        datapoints: [
                            {
                                timestamp: 1383230753238,
                                count: 50
                            },
                            {
                                timestamp: 1383230784165,
                                count: 0
                            },
                            {
                                timestamp: 1383230821680,
                                count: 156.54
                            }
                        ]
                    },
                    {
                        label: '国泰金鹰增长',
                        datapoints: [
                            {
                                timestamp: 1383230753238,
                                count: 70
                            },
                            {
                                timestamp: 1383230784165,
                                count: 180
                            },
                            {
                                timestamp: 1383230821680,
                                count: 90
                            }
                        ]
                    },
                    {
                        label: '华商主题精选股票',
                        datapoints: [
                            {
                                timestamp: 1383230753238,
                                count: 190
                            },
                            {
                                timestamp: 1383230784165,
                                count: 100
                            },
                            {
                                timestamp: 1383230821680,
                                count: 110
                            }
                        ]
                    }
                ]


                var barChartData = [
                    {label: '华安大中华升级', value: 156.54 },
                    {label: '长盛电子信息产业', value: 264.73},
                    {label: '长盛量化红利', value: 188.11 },
                    {label: '国泰金鹰增长', value: 186.69 },
                    {label: '华商主题精选股票', value: 168.47}
                ]

                var tempPieChart = angular.copy($scope.chartTemplate.pie);
                var tempLineChart = angular.copy($scope.chartTemplate.line);
                var tempAreaChart = angular.copy($scope.chartTemplate.area);
                var tempBarChart = angular.copy($scope.chartTemplate.bar);

                $scope.pieChartData = chartService.convertPieChart(pieChartData, tempPieChart, dataDescription.pie, '')
                $scope.lineChartData = chartService.convertLineChart(lineChartData, tempLineChart, dataDescription.timeseries, '');
                $scope.areaChartData = chartService.convertAreaChart(lineChartData, tempAreaChart, dataDescription.timeseries, '');
                $scope.barChartData = chartService.convertBarChart(barChartData, tempBarChart, dataDescription.bar, '');

            }

            function createGradient(color1, color2) {
                var perShapeGradient = {
                    x1: 0,
                    y1: 0,
                    x2: 0,
                    y2: 1
                };
                return {
                    linearGradient: perShapeGradient,
                    stops: [
                        [0, color1],
                        [1, color2]
                    ]
                }
            }


        }]);
    })